package com.codingday.app;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;

public class TODOListView extends ListActivity {
    /** Called when the activity is first created. */
	
	//ListView mTODOListView;
	//private static Context context;
	//TODOList list_todoListView;
	EditText et_SearchText;
	Button bt_Search;
	ListView lv;


	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.viewtodolist);

		//mTODOListView = (ListView) findViewById(R.id.todolist);

		
		//ArrayList<TODO> todolist = SaveManager.LoadAllTODO();
		ArrayList<TODO> todolist = TODOListManager.getTODOList();
        Log.i("TODOList",""+todolist.size());
        //TODOAdapter todoListAdapter = 
        lv = getListView();
        setListAdapter(new TODOListAdapter(this, R.layout.row_todo, todolist));
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                
        et_SearchText = (EditText) findViewById(R.id.todo_search_text);
        et_SearchText.setOnEditorActionListener(new OnEditorActionListener(){

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				// TODO Auto-generated method stub
				Search();
				return true;
			}
        	
        });

        bt_Search = (Button) findViewById(R.id.todo_search_button);
        bt_Search.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Search();
			}
        	
        });
    }
    
    
        
    @Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setListAdapter(new TODOListAdapter(this, R.layout.row_todo, TODOListManager.getTODOList()));
	}



	private void Search(){
		InputMethodManager imm = (InputMethodManager) getSystemService( Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(et_SearchText.getApplicationWindowToken(), 0); 
		
		String searchText = et_SearchText.getText().toString();
		
		if(searchText.length() == 0){
			//load all
			Toast.makeText(getApplicationContext(), "Show All Quests", 1).show();
	        setListAdapter(new TODOListAdapter(this, R.layout.row_todo, TODOListManager.getTODOList()));
			return;
		}
		
		ArrayList<TODO> searchedList = TODOListManager.getTODOList(searchText);
        setListAdapter(new TODOListAdapter(this, R.layout.row_todo, searchedList));
        Toast.makeText(getApplicationContext(), "Total "+searchedList.size()+" result(s)", 1).show();
    }

    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		Log.i("onActivityResult","requestCode:"+requestCode+" resultCode:"+resultCode);
		switch(requestCode){
			case 1:
				if(data.getBooleanExtra("result",false) == true){
					Log.i("Result is :","Saved" );
			        setListAdapter(new TODOListAdapter(this, R.layout.row_todo, TODOListManager.getTODOList()));
			        //lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
				}
				break;
			case 2:

				Log.i("GeoAlarm - onActivityResult()","Back From TODOView");
				break;
			default:
				break;
		}
	}



}

